import requests
import time
import hashlib
import json
import os
from requests.exceptions import RequestException

def md5(s):
    """返回字符串s的MD5值（32位小写）"""
    return hashlib.md5(s.encode('utf-8')).hexdigest()

def get_tkey():
    t = time.gmtime(time.time() + 8 * 3600)
    return time.strftime("%Y%m%d%H%M%S", t)
    # GET请求示例
    #test_get("https://httpbin.org/get", params={"key": "value"})
def test_get(url, params=None, headers=None):
    """测试GET接口"""
    try:
        response = requests.get(url, params=params, headers=headers)
        print("状态码:", response.status_code)
        print("响应内容:", response.text)
        return response
    except Exception as e:
        return "GET请求异常:", e

    # POST请求示例
    #test_post("https://httpbin.org/post", json={"key": "value"})
def test_post(url, data=None, json=None, headers=None):
    """测试POST接口"""
    try:
        response = requests.post(url, data=data, json=json, headers=headers)
        return response
    except Exception as e:
        return "POST请求异常:", e
# 单张卡详情查询
def query_card_info( userName, passWord, iccid=None, msisdn=None):
    if not iccid and not msisdn:
        return "请提供iccid或msisdn参数"
    url ="https://api.tibiot.com/api/v1/card/queryCardInfo"
    #tKey为24小时制的当前时间
    tKey=get_tkey()
    #密码加密
    pw=md5( md5(passWord) + tKey)
    data={"userName":userName,"passWord":pw,"tKey":tKey}
    if iccid:
        data["iccid"] = iccid
    else:
        data["msisdn"] = msisdn
    res = test_post(url, json=data)
    if isinstance(res, str):
        return res
    if res:
        response_data = json.loads(res.text)
        return response_data
# 单张卡在线信息实时查询
def card_online_information( userName, passWord, iccid=None, msisdn=None):
    if not iccid and not msisdn:
        return "请提供iccid或msisdn参数"

    url ="https://api.tibiot.com/api/v1/card/cardOnlineInformation"
    #tKey为24小时制的当前时间
    tKey=get_tkey()
    #密码加密
    pw=md5( md5(passWord) + tKey)
    data={"userName":userName,"passWord":pw,"tKey":tKey}
    if iccid:
        data["iccid"] = iccid
    else:
        data["msisdn"] = msisdn
    res = test_post(url, json=data)
    if isinstance(res, str):
        return res
    if res:
        response_data = json.loads(res.text)
        return response_data
# 单张卡本月流量使用量查询
def sim_data_margin( userName, passWord, iccid=None, msisdn=None):
    if not iccid and not msisdn:
        return "请提供iccid或msisdn参数"

    url ="https://api.tibiot.com/api/v1/card/simDataMargin"
    #tKey为24小时制的当前时间
    tKey=get_tkey()
    #密码加密
    pw=md5( md5(passWord) + tKey)
    data={"userName":userName,"passWord":pw,"tKey":tKey}
    if iccid:
        data["iccid"] = iccid
    else:
        data["msisdn"] = msisdn
    res = test_post(url, json=data)
    if isinstance(res, str):
        return res
    if res:
        response_data = json.loads(res.text)
        return response_data
#卡历史月用量查询
def query_card_month_data( userName, passWord, iccid=None, msisdn=None):
    if not iccid and not msisdn:
        return "请提供iccid或msisdn参数"

    url ="https://api.tibiot.com/api/v1/card/queryCardMonthData"
    #tKey为24小时制的当前时间
    tKey=get_tkey()
    #密码加密
    pw=md5( md5(passWord) + tKey)
    data={"userName":userName,"passWord":pw,"tKey":tKey}
    if iccid:
        data["iccid"] = iccid
    else:
        data["msisdn"] = msisdn
    res = test_post(url, json=data)
    if isinstance(res, str):
        return res
    if res:
        response_data = json.loads(res.text)
        return response_data
#卡批量信息查询
def query_card_batch_info( userName, passWord, iccid):
    url ="https://api.tibiot.com/api/v1/card/batchQueryCardInfo"
    #tKey为24小时制的当前时间
    tKey=get_tkey()
    #密码加密
    pw=md5( md5(passWord) + tKey)
    data={"userName":userName,"passWord":pw,"tKey":tKey,"cardType":1,"pageNum":1,"pageSize":100}
    if iccid:
        data["iccid"] = iccid
    res = test_post(url, json=data)
    if isinstance(res, str):
        return res
    if res:
        response_data = json.loads(res.text)
        return response_data